package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarDetail;
import com.yanqu.road.entity.activity.unionwar.cross.UnionWarPatronsData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUser;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_UNION_WAR_PREPARE_NO_DISPATCH_MEMBER, desc = "未派遣成员")
public class CrossUnionWarActivtyGetNoDispatchMemberCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        UnionWarActivityServerProto.UnionWarGetNoDispatchMemberReqMsg reqMsg = UnionWarActivityServerProto.UnionWarGetNoDispatchMemberReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        int phase = reqMsg.getPhase();
        int session = reqMsg.getSession();
        long playerId = packet.getPlayerId();
        String unionUid = Cross2UnionActivityGroupMgr.getUnionUid(activityId, playerId);

        if(!StringUtils.isNullOrEmpty(unionUid)){

            CrossUnionWarDetail detail = CrossUnionWarMgr.getCrossUnionWarDetailReadOnly(activityId, phase, session, unionUid);
            if(detail == null){
                return;
            }
            UnionActivityGroupUnion groupUnion = Cross2UnionActivityGroupMgr.getUnionData(activityId, unionUid);
            if(groupUnion == null){
                return;
            }

            List<Long> unionMemberList = new ArrayList<>();
            for (Map.Entry<Long, UnionActivityGroupUser> entry : groupUnion.getUserMap().entrySet()) {
                unionMemberList.add(entry.getKey());
            }

            UnionWarActivityServerProto.UnionWarGetNoDispatchMemberRespMsg.Builder respMsg = UnionWarActivityServerProto.UnionWarGetNoDispatchMemberRespMsg.newBuilder();
            respMsg.setActivityId(activityId);
            respMsg.setPhase(phase);
            respMsg.setSession(session);
            respMsg.setRet(0);


            List<Long> sendList = new ArrayList<>();
            for (int i = 0; i < detail.getPatronsDataList().size(); i++) {
                UnionWarPatronsData patronsData = detail.getPatronsDataList().get(i);
                long userId = patronsData.getUserId();
                if(!sendList.contains(userId)){
                    sendList.add(userId);
                }
            }
            for(Long userId : unionMemberList){
                if(!sendList.contains(userId)){
                    UnionWarActivityServerProto.UnionWarUnionMember.Builder builder = UnionWarActivityServerProto.UnionWarUnionMember.newBuilder();
                    builder.setUserId(userId);
                    UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(userId);
                    if (userBaseInfo == null) {
                        continue;
                    }
                    builder.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                    respMsg.addMember(builder);
                }
            }
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_WAR_PREPARE_NO_DISPATCH_MEMBER, respMsg));
        }

    }

    @Override
    public void sendData() {

    }
}
